/*
 ============================================================================
 Name		: fTimer.cpp
 Author	  : wangchao
 Version	 : 1.0
 Copyright   : 
 Description : CfTimer implementation
 ============================================================================
 */

#include "fTimer.h"

fTimer::fTimer( ) :
    CActive( EPriorityStandard ) // Standard priority
{
}

fTimer* fTimer::NewLC( )
{
    fTimer* self = new ( ELeave ) fTimer( );
    CleanupStack::PushL( self );
    self->ConstructL( );
    return self;
}

fTimer* fTimer::NewL( )
{
    fTimer* self = fTimer::NewLC( );
    CleanupStack::Pop( ); // self;
    return self;
}

void fTimer::ConstructL( )
{
    User::LeaveIfError( iTimer.CreateLocal( ) ); // Initialize timer
    CActiveScheduler::Add( this ); // Add to scheduler
}

fTimer::~fTimer( )
{
    Cancel( ); // Cancel any request, if outstanding
    iTimer.Close( ); // Destroy the RTimer object
    // Delete instance variables if any
}

void fTimer::DoCancel( )
{
    iTimer.Cancel( );
}

void fTimer::StartL( TTimeIntervalMicroSeconds32 aDelay )
{
    Cancel( ); // Cancel any request, just to be sure
    iState = EUninitialized;
    iTimer.After( iStatus, aDelay ); // Set for later
    SetActive( ); // Tell scheduler a request is active
}

void fTimer::RunL( )
{
    if( iState == EUninitialized )
    {
        // Do something the first time RunL() is called
        iState = EInitialized;
    }
    else if( iState != EError )
    {
        // Do something
    }
    iTimer.After( iStatus, 1000000 ); // Set for 1 sec later
    SetActive( ); // Tell scheduler a request is active
}

TInt fTimer::RunError( TInt aError )
{
    return aError;
}
